﻿@inherits PageLayout

<Layout SideWidth="0" IsFullSide ShowFooter ShowCollapseBar MenuBarIcon="fa fa-bars">
    <Header>
        <div class="kui-header">
            <Breadcrumb Value="@CrumbItems" />
            <TopNavRight OnMenuClick="OnMenuClick" />
        </div>
    </Header>
    <Side>
        <div class="boot-banner">
            <div class="boot-logo"></div>
        </div>
        <Menu DisableNavigation IsVertical IsAccordion Items="SideMenuItems" OnClick="OnSideMenuClick" />
    </Side>
    <Main>
        @if (Context.UserSetting.MultiTab)
        {
            
        }
        else
        {
            <div class="kui-page-body">@Body</div>
        }
    </Main>
    <Footer>
        @if (BootConfig.Option.Footer != null)
        {
            <div style="text-align:center">@BootConfig.Option.Footer</div>
        }
        else
        {
            <div style="text-align:center"><PageFooter /></div>
        }
    </Footer>
</Layout>

@code{
    private List<BreadcrumbItem> CrumbItems = [];
    private List<BootstrapBlazor.Components.MenuItem> SideMenuItems = [];

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        CrumbItems = GetBreadcrumbItems();
        SideMenuItems = UserMenus.ToSideMenuItems(Context);
    }

    private void OnHome() => OnMenuClick("home");

    private void OnMenuClick(string id)
    {
        switch (id)
        {
            case "logout":
                UI.Confirm(Language["Tip.Exits"], SignOutAsync);
                break;
        }
    }

    private Task OnSideMenuClick(BootstrapBlazor.Components.MenuItem item)
    {
        foreach (var menu in UserMenus)
        {
            foreach (var sub in menu.Children)
            {
                if (item.Id == sub.Id)
                {
                    CrumbItems = GetBreadcrumbItems(sub);
                    //Context.Navigate(sub);
                    break;
                }
            }
        }
        return Task.CompletedTask;
    }

    private List<BreadcrumbItem> GetBreadcrumbItems(MenuInfo item = null)
    {
        var items = new List<BreadcrumbItem>();
        items.Add(new BreadcrumbItem(Language.Home));
        if (item == null)
            return items;

        if (item.Parent != null)
        {
            var parentName = Language.GetString(item.Parent);
            items.Add(new BreadcrumbItem(parentName));
        }

        if (item.Name != Language.Home)
        {
            var menuName = Language.GetString(item);
            items.Add(new BreadcrumbItem(menuName));
        }

        return items;
    }
}